package com.garlic.face.jlf.ui;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.garlic.face.jlf.App;
import com.garlic.face.jlf.R;
import com.garlic.face.jlf.base.BaseActivity;
import com.garlic.face.jlf.presenter.CardPayContract;
import com.garlic.face.jlf.presenter.CardPayPresenter;
import com.garlic.face.jlf.transaction.Means;
import com.garlic.face.jlf.utils.LogUtils;
import com.garlic.face.jlf.utils.Tools;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

/**
 * Created by shiyanqi on 18/9/18.
 */

public class CardPayActivity extends BaseActivity implements CardPayContract.View{
	private String TAG = "CardPayActivity";
	private CardPayPresenter mPresenter;
	private CountDownTimer mCountDownTimer;
	private TextView mCancel;
	private ImageView mShopBrand;

	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.card_pay_activity);
		mPresenter = new CardPayPresenter(this);

		EventBus.getDefault().register(this);
		mCancel = (TextView) findViewById(R.id.cancel_pay);
		mCancel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View view) {
				View root = View.inflate(CardPayActivity.this, R.layout.close_dialog, null);
				AlertDialog.Builder builder = new AlertDialog.Builder(CardPayActivity.this).setView
						(root).setCancelable(false);
				final Dialog dialog = builder.create();
				dialog.setCanceledOnTouchOutside(false);
				dialog.show();
				root.findViewById(R.id.goto_agin).setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
					}
				});

				root.findViewById(R.id.cancel_action).setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						dialog.dismiss();
						showProgress();
						mPresenter.cancelPay("福卡支付页面取消交易[手动]");
					}
				});
			}
		});
		mCountDownTimer = new CountDownTimer(120 * 1000, 1000) {
			@Override
			public void onTick(long millisUntilFinished) {
				String s = String.format(getResources().getString(R.string.countdown_cancelPay), (int)millisUntilFinished / 1000);
				mCancel.setText(s);
			}

			@Override
			public void onFinish() {
				mPresenter.cancelPay("福卡支付页面取消交易[自动]");
			}
		}.start();

		TextView amount = (TextView) findViewById(R.id.amount);
		double price = Means.getInstance().getRec_Tran().getAmount() - Means.getInstance().getRec_Tran().getPay_amt();
		amount.setText(String.format(getResources().getString(R.string.money_format), Tools.getFormatNumber(price)));

		mShopBrand = (ImageView) findViewById(R.id.title_img);
		mShopBrand.setImageDrawable(this.getResources().getDrawable(R.mipmap.jlf_logo));

	}


	@Override
	protected void onResume() {
		super.onResume();
		showProgress(getResources().getString(R.string.brush_card));
		mPresenter.payCard();
	}

	@Override
	public void destoryTimer() {
		if (mCountDownTimer != null) {
			mCountDownTimer.cancel();
			mCountDownTimer = null;
		}
	}

	@Override
	public void toSuccssActivity(String result) {
		Intent intent = new Intent(CardPayActivity.this, PaySuccessActivity.class);
		intent.putExtra("scene", "card");
		startActivity(intent);
	}

	@Override
	public void toFailureActivity(String msg, String actionType) {
		Intent intent = new Intent(CardPayActivity.this, PayFailActivity.class);
		intent.putExtra("error_message", msg);
		intent.putExtra("action_type", actionType);
		startActivity(intent);
		finish();
	}

	@Override
	public void gotoMain() {
		App.clearToMainActivity();
	}

	@Override
	public void toPartialPaymentActivity(String remain) {
		Intent intent = new Intent(CardPayActivity.this, PartialPaymentActivity.class);
		intent.putExtra("remain", remain);
		startActivity(intent);
		finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		destoryTimer();
		EventBus.getDefault().unregister(this);
		mPresenter.destoryMXdevice();
	}

	@Subscribe(threadMode = ThreadMode.MAIN)
	public void onReceiveResponse(JSONObject jsonObject) {
		LogUtils.d("card支付返回,收到service消息 " + jsonObject.toString(), true);
		mPresenter.parseJson(jsonObject);
	}

}
